package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        List<Category> categoryList = categoryMapper.page(categoryPageQueryDTO);
        Page<Category> page = (Page<Category>) categoryList;
        PageResult pageResult = new PageResult(page.getTotal(),categoryList);
        return pageResult;
    }

    @Override
    public void add(Category category) {
       /* category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());*/
        category.setStatus(StatusConstant.ENABLE);
        /*category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());*/
        categoryMapper.add(category);
    }

    @Override
    public void status(Integer status, Long id) {
        categoryMapper.status(status,id);
    }

    @Override
    public void delete(Long id) {
        categoryMapper.delete(id);
    }

    @Override
    public List<Category> getByType(Integer type) {
        if(type != null) {
            return categoryMapper.getByType(type);
        }else {
            List<Category> list1 = categoryMapper.getByType(1);
            List<Category> list2 = categoryMapper.getByType(2);
            return ListUtils.union(list1,list2);
        }
    }

    @Override
    public void update(Category category) {
        /*category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());*/
        categoryMapper.update(category);
    }

}
